package com.fingard.dsp.bank.directbank.bony01;


import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ITransaction;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7515TodayTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7516HisTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.RetAtsTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.detail.RetAtsTransDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class BONY01Transaction extends BONY01Base implements ITransaction {

    @Override
    public RetAtsTrans d_7516_HisTrans(Req7516HisTrans p_req7516) throws Exception {
        //历史明细查询返回ats对象
        RetAtsTrans retAts = new RetAtsTrans(p_req7516.reqHeader);
        retAts.writeStart();// 写返回ATS的统一头部信息
        //赋值通用参数
        transCode = "010002";
        queryStartNo = "1";
        queryNum = "20";
        int nowReadedCounts = 0;//已读总笔数
        try {
            boolean tmpQryNext = true;
            while (tmpQryNext) {//分页查询
                //写入报文
                XmlTextWriter xmlWriter = new XmlTextWriter();
                writeCommonHeader(xmlWriter);
                xmlWriter.writeStartElement("Body");
                xmlWriter.writeElementString("AcctNo", p_req7516.actNum);
                xmlWriter.writeElementString("StartDate", p_req7516.dateFrom);
                xmlWriter.writeElementString("EndDate", p_req7516.dateTo);
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                //发送银行前置机
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                //赋值返回ats
                XmlTextReader tmpBkReader;
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String retCode = tmpBkReader.getSimpleText("RespCode");
                        String respMsg = tmpBkReader.getSimpleText("RespMsg");
                        int totQueryNum = Integer.parseInt(tmpBkReader.getSimpleText("TOtQueryNum"));//查询总笔数
                        int curQueryNum = Integer.parseInt(tmpBkReader.getSimpleText("CurQueryNum"));//本次查询返回笔数
                        if (totQueryNum == 0) {
                            //throw new Exception("银行返回总笔数为0");
                            retAts.writeRespCode("0");
                            retAts.writeRespInfo("银行返回明细为空");
                            break;
                        }
                        if (curQueryNum == 0) {
                            break;
                        }
                        if (retCode != null && isActiveRetCode(retCode)) {
                            while (tmpBkReader.read("/Root/Body/QueryList")) {
                                RetAtsTransDetail tmpRetDetail = new RetAtsTransDetail();
                                String tranDate = tmpBkReader.readedXElement.getSingleText("TxDate");//yyyymmdd 系统格式为yyyy-MM-dd hh:mm:ss
                                String tranTime = tmpBkReader.readedXElement.getSingleText("TxTime");//HHMMSS
                                tmpRetDetail.tranDate = tranDate.substring(0, 4) + "-" + tranDate.substring(4, 6) + "-" + tranDate.substring(6);
                                tmpRetDetail.tranTime = tmpRetDetail.tranDate + " " + tranTime.substring(0, 2) + ":" + tranTime.substring(2, 4) + ":" + tranTime.substring(4);
                                tmpRetDetail.valueDate = tmpRetDetail.tranDate;//起息日没有返回就用交易日
                                tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("AcctNo");//账号
                                if (!tmpRetDetail.actNum.equals(p_req7516.actNum)) {
                                    String tmpInfo = "银行返回的账号" + tmpRetDetail.actNum + "与请求账号" + p_req7516.actNum + "不符";
                                    retAts.writeRespCode("-1");
                                    retAts.writeRespInfo(tmpInfo);
                                    break;
                                }
                                tmpRetDetail.accBal = tmpBkReader.readedXElement.getSingleText("AcctBal");//账户余额
                                tmpRetDetail.cur = p_req7516.actCur; //tmpBkReader.readedXElement.getSingleText("CurCode");//币种
                                tmpRetDetail.recAccNum = tmpBkReader.readedXElement.getSingleText("OthAcctNo");//对方账号
                                tmpRetDetail.recAccName = tmpBkReader.readedXElement.getSingleText("OthAcctName");//对方户名
                                tmpRetDetail.usage = tmpBkReader.readedXElement.getSingleText("Abs");//摘要
                                tmpRetDetail.abstr = pickupAbstr(tmpRetDetail.usage);
                                tmpRetDetail.comment = tmpBkReader.readedXElement.getSingleText("Remark");//备注
                                //借贷标志 1-借 2-贷 3-收 4-付 todo 银行返回的借贷标识有4种，ats只有支出1和收入2
                                tmpRetDetail.tranAmt = tmpBkReader.readedXElement.getSingleText("TxAmt");//交易金额
                                String dcFlag = tmpBkReader.readedXElement.getSingleText("DcFlag");
                                switch (Integer.parseInt(dcFlag)) {
                                    case 1: {//借
                                        if (Double.parseDouble(tmpRetDetail.tranAmt) > 0) {
                                            tmpRetDetail.cdFlag = "1";
                                        } else {
                                            tmpRetDetail.cdFlag = "2";
                                        }
                                        break;
                                    }
                                    case 2: {//贷
                                        if (Double.parseDouble(tmpRetDetail.tranAmt) < 0) {
                                            tmpRetDetail.cdFlag = "1";
                                        } else {
                                            tmpRetDetail.cdFlag = "2";
                                        }
                                        break;
                                    }
                                    case 3: {//收
                                        if (Double.parseDouble(tmpRetDetail.tranAmt) < 0) {
                                            tmpRetDetail.cdFlag = "1";
                                        } else {
                                            tmpRetDetail.cdFlag = "2";
                                        }
                                        break;
                                    }
                                    case 4: {//付
                                        if (Double.parseDouble(tmpRetDetail.tranAmt) > 0) {
                                            tmpRetDetail.cdFlag = "1";
                                        } else {
                                            tmpRetDetail.cdFlag = "2";
                                        }
                                        break;
                                    }
                                    default:
                                        retAts.writeRespCode("-1");
                                        retAts.writeRespInfo("银行返回借贷标志无法识别，请核对！");
                                        retAts.writeEnd();
                                        return retAts;
                                }
                                tmpRetDetail.tranAmt = tmpRetDetail.tranAmt.replace("-", "");
                                retAts.writeRetDetail(tmpRetDetail);
                            }
                            nowReadedCounts += curQueryNum;
                            if (nowReadedCounts == totQueryNum) {//本次分页查询完毕
                                tmpQryNext = false;
                            } else {
                                queryStartNo = String.valueOf(nowReadedCounts + 1);
                            }
                        } else {
                            tmpQryNext = false;
                            getRetInfo(retAts, retCode, respMsg);
                        }
                    } else {
                        tmpQryNext = false;
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpSbMsg.toString());
                    }
                } else {
                    tmpQryNext = false;
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpStrRet[1]);
                }
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsTrans d_7515_TodayTrans(Req7515TodayTrans p_req7515) throws Exception {
        Req7516HisTrans tmpReq7516 = new Req7516HisTrans(p_req7515.reqHeader);
        tmpReq7516.actNum = p_req7515.actNum;
        String tmpStrToday = Format.Date8Format.format(new Date());
        tmpReq7516.dateFrom = tmpStrToday;
        tmpReq7516.dateTo = tmpStrToday;
        return d_7516_HisTrans(tmpReq7516);
    }
}
